<?php

namespace App\Admin\Forms;

use App\Models\MessageRecord;
use App\Models\Order;
use App\Models\OrderServiceProfile;
use App\Models\UsersAuth;
use Carbon\Carbon;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;


class ReviewStatus extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;
        if (!$id) {
            return $this->response()->error('参数错误');
        }
        $userAuth = UsersAuth::where('user_id', $id)->first();

        if (empty($userAuth)) {
            return $this->response()->error('未找到认证信息');
        }
        $userAuth->examine_time = $input['examine_time'] ?? '';
        $userAuth->review_status = $input['review_status'] ?? '';
        $userAuth->reason = $input['reason'] ?? '';
        $result = $userAuth->save();

        if ($result) {
            return $this->response()->success('成功')->refresh();
        } else {
            return $this->response()->error('失败');
        }
    }

    public function form()
    {
        $id = $this->payload['id'] ?? null;
        // dd($userid);
        $this->hidden('examine_time')->value(Carbon::now());
        $this->select('review_status', '审核状态')->options(['0' => '未审核', '1' => '审核通过', '2' => '审核驳回'])->when(
            [1, 2],
            function (Form $form) {
                $form->textarea('reason', '审核意见');
            }
        );
    }

    // 返回表单数据，如不需要可以删除此方法
    public function
    default()
    {
        return [
            'review_status' => '',
            'reason'         => '', // 订单编号
            'examine_time' => '',
        ];
    }
}
